package com.ikkong.platform.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.ikkong.common.vo.User;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.platform.model.Branch;
import com.ikkong.platform.model.Info;
import com.ikkong.platform.model.News;
import com.ikkong.platform.model.Plan;
import com.ikkong.platform.service.InfoService;
import com.ikkong.platform.service.impl.InfoServiceImpl;
import com.ikkong.system.controller.base.UrlPermissController;
import com.jfinal.upload.UploadFile;
import org.beetl.sql.core.kit.StringKit;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Generated by Blade.
 * 2018-09-14 21:12:29
 */
public class APIController extends UrlPermissController {
	private static String CODE = "info";
	private static String PERFIX = "info";
	private static String LIST_SOURCE = "Info.list";
	private static String BASE_PATH = "/platform/info/";
	
	InfoService service = new InfoServiceImpl();

	public void index() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
		setAttr("code", CODE);
		render(BASE_PATH + "info.html");
	}
    public void toexcel() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        setAttr("code", CODE);
        render(BASE_PATH + "toexcel.html");
    }

    //临时
    public void temp() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        List<Branch> all = Blade.create(Branch.class).findAll();
        for(Branch b:all){
            if(b!=null){
                Plan plan = new Plan();
                plan.setB_id(b.getId());
                plan.setPlan("批量导入的工作计划------"+b.getName());
                plan.setYearmonth("201809");
                Blade.create(Plan.class).save(plan);
            }
        }
        renderText("测试成功");
    }

    //导入 接口
    public void importuser() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        UploadFile file = getFile();
        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(1);
        int up = 0;
        int sav = 0;

        String path = file.getUploadPath()+"\\"+file.getFileName();
        File f = new File(path);
        List<Info> list = ExcelImportUtil.importExcel(f,Info.class, params);
        for(Info info :list){
            boolean save = Blade.create(Info.class).save(info);
            if(save){
                sav++;
            }
        }

        renderText(sav+"");
    }




    public void echartsEdu() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        List<Map> select = Blade.dao().select("Info.countedu", Map.class, Record.create());
        List name = new ArrayList();
        List num = new ArrayList();
        for(Map map :select){
            name.add(map.get("name"));
            num.add(map.get("num"));
        }
        Map map = new HashMap();
        map.put("names",name);
        map.put("nums",num);
        map.put("maps",select);
        renderJson(JsonKit.toJson(map));
    }

    public void echartsYear() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        List<Map> select = Blade.dao().select("Info.countyear", Map.class, Record.create());
        renderJson(JsonKit.toJson(select));
    }

    public void mappoint() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        String fenbu = getPara("fenbu", "");
        Map para = new HashMap();
        if(StringKit.isNotBlank(fenbu)){
            para.put("pid",fenbu);
        }
        List<Map> select = Blade.dao().select("Info.countmap", Map.class, para);
        renderJson(JsonKit.toJson(select));
    }


	public void newsList() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        String type = getPara("type", "1");
        int page = getParaToInt("page",1);
        int start = (page-1) * 10 + 1;
        List<Map> select = Blade.dao().select("News.list", Map.class, Record.create().set("type",type), start, 10);
        List<Map> all = Blade.dao().select("News.list", Map.class, Record.create().set("type",type));
        int rows = all.size();
        int pages = (int)Math.ceil((double) rows/10);
        Map map = new HashMap();
        map.put("records",rows);
        map.put("pages",pages);
        map.put("thispage",page);
        map.put("list",select);
        renderJson(map);
    }


    //新闻详情
    public void newsDetail() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        int id = getParaToInt("id",1);
        Map select = Blade.dao().selectSingle("News.one", Record.create().set("id",id), Map.class);
        renderJson(select);
    }


    //支部列表
    public void branchList() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        List<Map> all = Blade.dao().select("Branch.list", Map.class, Record.create());
        List list = new ArrayList();
        for(Map b:all){
            if((int)b.get("p_id") == 0){
                List<Branch> by = Blade.create(Branch.class).findBy("p_id = " + b.get("id"), Branch.class);
                b.put("child",by);
                list.add(b);
            }
        }
        renderJson(list);
    }


    //党费缴纳
    public void humanList() {


        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        int page = getParaToInt("page",1);
        int start = (page-1)*16+1;
        List<Map> all = Blade.dao().select("Info.ismoney", Map.class, Record.create(),start,16);
        List<Map> aa = Blade.dao().select("Info.ismoney", Map.class, Record.create());
        Map map = new HashMap();
        map.put("page",page);
        map.put("count", aa.size());
        map.put("list",all);
        renderJson(map);
    }


    //支部详情
    public void branchDetail() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        String id = getPara("id","8");
        Branch byId = Blade.create(Branch.class).findById(id);
        List<String> info = Blade.create(Info.class).findBy("name","d_id = " + id+" order by id desc", String.class);
        Map plan = Blade.dao().selectSingle("Info.plan", Record.create().set("id",id), Map.class);
        Map map = new HashMap();
        String name = byId.getName();
        map.put("name",name);
        map.put("leader",byId.getLeader());
        map.put("user",info);
        map.put("plan",plan);


        renderJson(map);
    }


    //支部三会一课
    public void shykByBranch() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        String type = getPara("type", "1");
        //支部id
        String id = getPara("id");
        int page = getParaToInt("page",1);
        int start = (page-1) * 10 + 1;
        List<Map> select = Blade.dao().select("News.list", Map.class, Record.create().set("type",type).set("bid",id), start, 10);
        List<Map> all = Blade.dao().select("News.list", Map.class, Record.create().set("type",type).set("bid",id));
        int rows = all.size();
        int pages = (int)Math.ceil((double) rows/10);
        Map map = new HashMap();
        map.put("records",rows);
        map.put("pages",pages);
        map.put("thispage",page);
        map.put("list",select);
        renderJson(map);;
    }

    //新闻首页
    public void newsindex() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        List<Map> lunbo = Blade.dao().select("Info.lunbo", Map.class, Record.create());
        List<Map> shyk = Blade.dao().select("Info.shykimg", Map.class, Record.create());
        Map map  = new HashMap();
        map.put("lunbo",lunbo);
        map.put("shyk",shyk);
        renderJson(map);
    }

    //工作计划
    public void plan() {
        this.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        this.getResponse().addHeader("Access-Control-Allow-Headers", "*");
        this.getResponse().setHeader("Access-Control-Allow-Credentials", "true");
        String id = getPara("id");
        Map map = Blade.dao().selectSingle("Info.plan", Record.create().set("id",id), Map.class);
        renderJson(map);
    }

	public void update() {
		Info info = mapping(PERFIX, Info.class);
		boolean temp = service.update(info);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

}
